import java.util.ArrayList;

public class StringCalculator {

    public int add(){
        return 0;
    }

    private String[] delimit(String number){
        if (number.startsWith("//[")) {
            String delimiter = "";
            delimiter = number.split("]\n")[0].substring(3);
            number = number.substring(delimiter.length()+5);
            String[] delimiterList = delimiter.split("]\\[");
            for(String del : delimiterList){
                number = number.replaceAll("\\Q" + del + "\\E", ",");
            }
        }
        return number.split("[,\n]");
    }

    private String[] checkForNegatives(String[] number) throws NegativeNumberException{
        Boolean hasNegatives = false;
        ArrayList<String> negatives = new ArrayList<String>();
        for(String num : number) {
            if(num.startsWith("-")) {
                hasNegatives = true;
                negatives.add(num);
            }
        }
        if(hasNegatives){
            String message = "Negatives not allowed:" + negatives.toString().replaceAll("\\[",",").replaceAll("]","").substring(1).replaceAll(" ","");
            NegativeNumberException negex = new NegativeNumberException(message);
            throw negex;
        }
        return number;
    }

    private int returnSum(String[] number){
        int total = 0;
        for(String num : number) {
            if (Integer.parseInt(num) < 1001){
                total += Integer.parseInt(num);
            }
        }
        return total;
    }

    public int add(String number) throws NegativeNumberException{
        String[] delimitedNumber = delimit(number);
        checkForNegatives(delimitedNumber);
        return returnSum(delimitedNumber);
    }
}
